package com.personal.system.service.impl.approve;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal.model.system.SysUser;
import com.personal.system.entity.approval.AddSignVo;
import com.personal.system.entity.approval.ApproveDetail;
import com.personal.system.exception.GlobalException;
import com.personal.system.mapper.act.ApproveDetailMapper;
import com.personal.system.service.sys.SysUserService;
import com.personal.system.service.approve.ApproveDetailService;
import com.personal.system.service.approve.ApproveWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ApproveDetailServiceImpl extends ServiceImpl<ApproveDetailMapper, ApproveDetail> implements ApproveDetailService {

    @Resource
    private ApproveDetailMapper approveDetailMapper;

    @Resource
    private ApproveWorkService workService;

    @Resource
    private SysUserService sysUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddSign(AddSignVo addSignVO) {
        log.info("进入到批量加签方法:{}",addSignVO);
        // 业务ID
        List<String> businessIds = addSignVO.getBusinessIds();
        // 数据集合
        List<ApproveDetail> approveDetails = addSignVO.getApproveDetails();
        // 加签方向
        String addSignDirection = addSignVO.getAddSignDirection();

        for (String businessId : businessIds) {
            List<ApproveDetail> details = approveDetailMapper.selectList(Wrappers.lambdaQuery(ApproveDetail.class).eq(ApproveDetail::getApproveNo, businessId));
            if (CollUtil.isEmpty(details)) throw new GlobalException(404,"需要加签的数据不存在!");

            // 当前审核人数据
            ApproveDetail approveDetail = null;
            List<String> userList = new ArrayList<>();
            for (ApproveDetail detail : details) {
                if (detail.getApprovalStatus().equals("02")) {
                    approveDetail = detail;
                }
                if ("02".equals(detail.getAddSignType())) {
                    String approverType = detail.getApproverType();
                    String approver = detail.getApprover();
                    if (approverType.equals("01")) {
                        userList.add(approver);
                    }
                }
            }

            for (ApproveDetail item : approveDetails) {
                String approverType = item.getApproverType();
                String approver = item.getApprover();
                if (approverType.equals("01")) {
                    if (userList.contains(approver)) {
                        throw new GlobalException(404,"加签的用户在业务链条中存在!");
                    }
                }
            }

            // 根据向下向上决定修改条件
            String approvalStatus = addSignDirection.equals("01")?"01":"01,02";
            // 更新原有审批的排序:原来值+加的数量
            approveDetailMapper.updateSort(approveDetails.size(), approvalStatus, businessId );

            if (addSignDirection.equals("01")) {
                log.info("开始向下加签>");
                workService.autoApproveForAddSign(approveDetail, addSignVO.getOperationUser());
            } else {
                // 向上加签，原来审批中变待审批
                ApproveDetail record = new ApproveDetail();
                record.setId(approveDetail.getId());
                record.setApprovalStatus("01");
                record.setUpdater(addSignVO.getOperationUser());
                record.setUpdateTime(new Date());
                approveDetailMapper.updateById(record);
            }
            Integer approvalSort = approveDetail.getApprovalSort();
            // 新增数据
            for (int i = 0; i < approveDetails.size(); i++) {
                ApproveDetail detail = approveDetails.get(i);
                detail.setId(UUID.fastUUID().toString(true));
                detail.setApproveNo(businessId);
                detail.setAddSignType("02");
                if (addSignDirection.equals("01")) {
                    // 如果是向下,从原来审批人的顺序上加1
                    detail.setApprovalSort(approvalSort + (i + 1));
                    if(i == 0) {
                        // 第一个加签人状态是审批中
                        detail.setApprovalStatus("02");
                    } else {
                        detail.setApprovalStatus("01");
                    }
                } else {
                    // 如果是向上
                    if (i == 0) {
                        detail.setApprovalSort(approvalSort);
                        detail.setApprovalStatus("02");
                    } else {
                        detail.setApprovalSort(approvalSort + i);
                        detail.setApprovalStatus("01");
                    }
                }

                String approverType = detail.getApproverType();
                String approver2 = detail.getApprover();
                // 用户类型
                if ("01".equals(approverType)) {
                    SysUser userInfo = sysUserService.getById(approver2);
                    if (userInfo != null) {
                        // 审核人
                        detail.setApprover(approver2);
                        detail.setApproverName(userInfo.getName());
                        detail.setDepartName(null==userInfo.getDeptName()?"":userInfo.getDeptName());
                    }
                    // 用户类型
                    detail.setApproverType("01");

                    // 角色类型
                }

                // 原始信息
                detail.setType(approveDetail.getType());
                detail.setRuleNo(approveDetail.getRuleNo());
                detail.setRuleClass(approveDetail.getRuleClass());
                detail.setCreater(addSignVO.getOperationUser());
                detail.setCreateTime(new Date());
                approveDetailMapper.insert(detail);

            }
        }

    }

}
